﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Abp.UI;
using SingleUserMall.Common;
using SingleUserMall.Headline;
using SingleUserMall.HeadLine.Form;
using SingleUserMall.HeadLine.VO;

namespace SingleUserMall.HeadLine
{
    public class HeadLinesManageAppService : SingleUserMallAppServiceBase,IHeadLinesManageAppService
    {
        private readonly IRepository<HeadLines, long> _headLinesRepository;
        public HeadLinesManageAppService(
            IRepository<HeadLines, long> headLinesRepository
    )
        {
            _headLinesRepository = headLinesRepository;
        }
        /// <summary>
        /// 查询所有头条文章列表
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<headlinVO>> GetAllHeadLinesAsync(SerchForm form)
        {
            var headlines = _headLinesRepository.GetAll().WhereIf(!string.IsNullOrEmpty(form.SerchKey), x => x.Title.Contains(form.SerchKey)).OrderByDescending(x => x.CreationTime);
            return await headlines.ToPagedResultDtoAsync<HeadLines, headlinVO>(form, (c, vo) => { });
        }
        /// <summary>
        /// 查询头条文章详情
        /// </summary>
        /// <param name="ArticleId"></param>
        /// <returns></returns>
        public async Task<headlineDetailVO> GetArticleDetailAsync(long ArticleId)
        {
            var article = await _headLinesRepository.FirstOrDefaultAsync(x => x.Id == ArticleId);
            return article.ToVO<HeadLines, headlineDetailVO>();
        }
        /// <summary>
        /// 删除文章信息
        /// </summary>
        /// <param name="ArticleId"></param>
        /// <returns></returns>
        public async Task DeleteArticleAsync(long ArticleId)
        {
            var article = await _headLinesRepository.FirstOrDefaultAsync(x => x.Id == ArticleId);
            await _headLinesRepository.DeleteAsync(article);
        }
        /// <summary>
        /// 保存今日头条信息
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public async Task SaevHeadlineAsync(HeadlineForm form)
        {
            try
            {
                var headline = await _headLinesRepository.FirstOrDefaultAsync(form.Id);
                if (headline != null)
                {
                    headline = form.MapTo(headline);
                    await _headLinesRepository.UpdateAsync(headline);
                }
                else
                {
                    // headline = form.MapTo<HeadLines>();
                    headline = new HeadLines
                    {
                        Title = form.Title,
                        Author = form.Author,
                        ArticleCover = form.ArticleCover,
                        AllAreaCode = form.AllAreaCode,
                        AreaCode = form.AreaCode,
                        Content = form.Content,
                    };
                    await _headLinesRepository.InsertAsync(headline);
                }
                await CurrentUnitOfWork.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw new UserFriendlyException(e.Message);
            }
        }
    }
}
